package com.rpa.server.servicerpaserver.expose.controller;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.rpa.secure.servicerpasecure.common.result.Result;
import com.rpa.secure.servicerpasecure.common.util.CurrentTeacherInfoUtil;
import com.rpa.server.servicerpaserver.crawler.entity.User;
import com.rpa.server.servicerpaserver.crawler.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author 86152
 * @Date 2025/2/10 20:27
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/user")
@Slf4j
@Tag(name = "本系统用户信息相关接口", description = "UserController")
public class UserController {
    @Autowired
    private UserService userService;
    @Operation(summary = "更具id获取用户信息", description = "getUser")
    @PostMapping("/getUser")
    public Result<User> getUser(){
        User byId = userService.getById(CurrentTeacherInfoUtil.getCurrentTeacherId());
        return Result.success(byId);
    }

    @Operation(summary = "修改用户信息", description = "updateUser")
    @PostMapping("/updateUser")
    public Result<User> updateUser(@RequestBody User user){
        boolean update = new LambdaUpdateChainWrapper<User>(userService.getBaseMapper())
                .eq(User::getId, CurrentTeacherInfoUtil.getCurrentTeacherId())
                .set(User::getXingAccount, user.getXingAccount())
                .set(User::getXingPassword, user.getXingPassword())
                .set(User::getXingName, user.getXingName())
                .set(User::getStuAccount, user.getStuAccount())
                .set(User::getStuPassword, user.getStuPassword())
                .update();
        if (update){
            User byId = userService.getById(CurrentTeacherInfoUtil.getCurrentTeacherId());
            return Result.success(byId);
        }
        return Result.error("修改失败");
    }
}
